﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using eWallet.Collections.Generic;

namespace eWallet.Windows.Controls
{
	/// <summary>
	/// Interaction logic for TagCloud.xaml
	/// </summary>
	public partial class TagCloud : UserControl
	{
		public TagCloud()
		{
			InitializeComponent();

			this.BreadCrumb.ItemsChanged += delegate(object sender, EventArgs e)
			{
				this.OnSelectedLabelsChanged(e);
			};

		}

		private EventHandler SelectedLabelsChangedHandler;
		public event EventHandler SelectedLabelsChanged
		{
			add { this.SelectedLabelsChangedHandler += value; }
			remove { this.SelectedLabelsChangedHandler -= value; }
		}

		private EventHandler<ItemClickedEventArgs> TagClickedHandler;
		public event EventHandler<ItemClickedEventArgs> TagClicked
		{
			add { this.TagClickedHandler += value; }
			remove { this.TagClickedHandler -= value; }
		}



		public string BreadCrumbSeparator
		{
			get { return this.BreadCrumb.Separator; }
			set { this.BreadCrumb.Separator = value; }
		}

		public string InitialSelectedLabel
		{
			get { return this.BreadCrumb.HomeLabelText; }
			set { this.BreadCrumb.HomeLabelText = value; }
		}

		private EventfulList<Tag> _Items;
		public IList<Tag> Items
		{
			get
			{
				if (null == _Items)
				{
					_Items = new EventfulList<Tag>();
					_Items.Added += delegate(object sender, ListItemEventArgs<Tag> e)
					{
						Label label = new Label();
						label.Tag = e.Item;
						label.Cursor = Cursors.Hand;
						label.MouseLeftButtonUp += this.Tag_MouseLeftButtonUp;
						label.Content = string.Format("{0} ({1})", e.Item.Label, e.Item.ItemCount);
						this.TagsPanel.Children.Add(label);
					};

					_Items.Cleared += delegate(object sender, EventArgs e)
					{
						this.TagsPanel.Children.Clear();
					};

					_Items.Removed += delegate(object sender, ListItemEventArgs<Tag> e)
					{
						this.TagsPanel.Children.RemoveAt(e.Index);
					};
				}
				return _Items;
			}
		}

		public IEnumerable<string> SelectedLabels
		{
			get { return this.GetSelectedLabels(); }
		}

		public bool ShowBreadCrumbPath
		{
			get { return this.BreadCrumbPanel.Visibility == Visibility.Visible; }
			set
			{
				if (value)
				{
					this.BreadCrumbPanel.Visibility = Visibility.Visible;
				}
				else
				{
					this.BreadCrumbPanel.Visibility = Visibility.Collapsed;
				}
			}
		}

		public bool ShowInitialSelectedLabel
		{
			get { return this.BreadCrumb.ShowHomeLabel; }
			set { this.BreadCrumb.ShowHomeLabel = value; }
		}



		public void ClearSelection()
		{
			if (this.BreadCrumb.Items.Count > 0)
			{
				this.BreadCrumb.Items.Clear();
				this.OnSelectedLabelsChanged(EventArgs.Empty);
			}
		}



		protected virtual void OnSelectedLabelsChanged(EventArgs e)
		{
			if (null != this.SelectedLabelsChangedHandler)
			{
				this.SelectedLabelsChangedHandler(this, e);
			}
		}

		protected virtual void OnTagClicked(ItemClickedEventArgs e)
		{
			if (null != this.TagClickedHandler)
			{
				this.TagClickedHandler(this, e);
			}
		}



		private IEnumerable<string> GetSelectedLabels()
		{
			List<string> list = new List<string>();

			foreach (var item in this.BreadCrumb.Items)
			{
				list.Add(item);
			}

			return list;
		}

		private void Tag_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			Label label = (Label)sender;

			for (int i = 0; i < this.TagsPanel.Children.Count; i++)
			{
				Label l1 = (Label)this.TagsPanel.Children[i];
				if (object.ReferenceEquals(label, l1))
				{
					this.BreadCrumb.Items.Add(this.Items[i].Label);
					this.OnTagClicked(new ItemClickedEventArgs(i));
					break;
				}
			}
		}

	}
}
